/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import entities.UserProject;
import interfaces.UserProjectInterface;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Thibault Manage header informations and login/logout
 */
@ManagedBean
@SessionScoped
public class HeaderBean implements Serializable {

    private static final long serialVersionUID = 1L;
    @EJB
    UserProjectInterface userEJB;
    private boolean logged;
    private UserProject loggedUser;
    private String login;
    private String password;

    public void login() {
        logged = true;
        try {
            loggedUser = userEJB.findUserByLoginAndPassword(login, password);
        } catch (EJBException EJBex) {
            logged = false;
            FacesMessage msg = new FacesMessage("Login failed");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
        login = "";
        password = "";
    }

    public void logout() {
        NavigationBean nb = (NavigationBean) FacesContext.getCurrentInstance().getExternalContext()
                .getSessionMap().get("navigationBean");
        logged = false;
        loggedUser = null;
        ((HttpSession) FacesContext.getCurrentInstance()
                .getExternalContext().getSession(true)).invalidate();
        nb.setPage("/default.xhtml");
    }

    public boolean isLogged() {
        return logged;
    }

    public void setLogged(boolean logged) {
        this.logged = logged;
    }

    public UserProject getLoggedUser() {
        return loggedUser;
    }

    public void setLoggedUser(UserProject loggedUser) {
        this.loggedUser = loggedUser;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
